package com.magicsoft.share.ui.share;


import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.ShareFragmentAdapter;
import com.magicsoft.share.base.BaseFragment;
import com.magicsoft.share.bean.my.MessageRead;
import com.magicsoft.share.bean.share.ArticleType;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.presenter.share.SharePresenter;
import com.magicsoft.share.ui.my.MessageActivity;
import com.magicsoft.share.view.share.IShareView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShareFragment extends BaseFragment<IShareView, SharePresenter> implements IShareView {


    @BindView(R.id.tv_title_left)
    TextView tvTitleLeft;
    @BindView(R.id.iv_msg)
    ImageView ivMsg;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPage)
    ViewPager viewPage;
    Unbinder unbinder;
    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.view_notice)
    View viewNotice;
    @BindView(R.id.layout_msg)
    RelativeLayout layoutMsg;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    private View view;
    private ShareFragmentAdapter shareFragmentAdapter;

    private ArrayList<Fragment> fragments = new ArrayList<>();
    private int selectedTabPosition = 0;
    private List<ArticleType> mArticleTypes;
    private ArticleType selectedArticleType;
    private int selectedArticleTypeId;
    private boolean isFirst = true;

    public ShareFragment() {
        // Required empty public constructor
    }


    @Override
    public void initView() {


        initTab();

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {


            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                isFirst = false;

                requestData();
                selectedTabPosition = tabLayout.getSelectedTabPosition();
            }
        });


    }


    private void initTab() {


        shareFragmentAdapter = new ShareFragmentAdapter(getChildFragmentManager());
        viewPage.setAdapter(shareFragmentAdapter);
        tabLayout.setupWithViewPager(viewPage);
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_share;
    }

    @Override
    public SharePresenter initPresenter() {
        return new SharePresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void requestData() {

        mPresenter.articleTypeList();
        mPresenter.messageRead();
        mPresenter.checkLogin();
    }

    public void refreshData() {
        selectedTabPosition = tabLayout.getSelectedTabPosition();
        if (mArticleTypes != null) {
            selectedArticleType = mArticleTypes.get(selectedTabPosition);
            selectedArticleTypeId = selectedArticleType.getId();
        }
        requestData();
    }

    @OnClick({R.id.iv_msg, R.id.tv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_msg:
                startActivityForResult(new Intent(getActivity(), MessageActivity.class), Constant.REFRESH_UI);
                break;
            case R.id.tv_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }

    @Override
    public void articleTypeListSuccess(List<ArticleType> articleTypes) {


        if (isFirst) {
            shareFragmentAdapter.setData(articleTypes);
        } else {

            shareFragmentAdapter.refreshData(articleTypes, selectedTabPosition);

        }
        refreshLayout.finishRefresh();

    }

    @Override
    public void messageRead(MessageRead data) {

        if (data.getAll_read_type() == 1) {
            viewNotice.setVisibility(View.VISIBLE);
        } else {
            viewNotice.setVisibility(View.GONE);
        }
    }


}
